import requests
import json
import uuid
from utils.logger import logger
from config.config import config

class APIClient:
    """
    API请求客户端，封装了常用的HTTP请求方法
    """
    def __init__(self):
        self.base_url = config.get_base_url()
        self.session = requests.Session()
        self.token = None
        self.uuid_val = str(uuid.uuid4())
    
    def get_verification_code(self, func='login'):
        """
        获取验证码
        :param func: 功能类型，login或register
        :return: 验证码图片响应
        """
        url = f"{self.base_url}/user/verification/{func}/{self.uuid_val}/"
        response = self.session.get(url)
        logger.info(f"获取验证码: {url}, 状态码: {response.status_code}")
        return response
    
    def login(self, username, password, verify_code="1234"):
        """
        用户登录
        :param username: 用户名
        :param password: 密码
        :param verify_code: 验证码
        :return: 登录响应
        """
        url = f"{self.base_url}/user/login/"
        payload = {
            "username": username,
            "password": password,
            "verify": verify_code,
            "uuid": self.uuid_val
        }
        response = self.session.post(url, json=payload)
        logger.info(f"登录请求: {url}, 状态码: {response.status_code}")
        
        if response.status_code == 200:
            self.token = response.json().get('access')
            self.session.headers.update({
                'Authorization': f'Bearer {self.token}'
            })
        
        return response
    
    def get(self, endpoint, params=None):
        """
        发送GET请求
        :param endpoint: API端点
        :param params: 查询参数
        :return: 响应对象
        """
        url = f"{self.base_url}{endpoint}"
        response = self.session.get(url, params=params)
        logger.info(f"GET请求: {url}, 状态码: {response.status_code}")
        return response
    
    def post(self, endpoint, data=None, json_data=None):
        """
        发送POST请求
        :param endpoint: API端点
        :param data: 表单数据
        :param json_data: JSON数据
        :return: 响应对象
        """
        url = f"{self.base_url}{endpoint}"
        response = self.session.post(url, data=data, json=json_data)
        logger.info(f"POST请求: {url}, 状态码: {response.status_code}")
        return response
    
    def put(self, endpoint, data=None, json_data=None):
        """
        发送PUT请求
        :param endpoint: API端点
        :param data: 表单数据
        :param json_data: JSON数据
        :return: 响应对象
        """
        url = f"{self.base_url}{endpoint}"
        response = self.session.put(url, data=data, json=json_data)
        logger.info(f"PUT请求: {url}, 状态码: {response.status_code}")
        return response
    
    def delete(self, endpoint):
        """
        发送DELETE请求
        :param endpoint: API端点
        :return: 响应对象
        """
        url = f"{self.base_url}{endpoint}"
        response = self.session.delete(url)
        logger.info(f"DELETE请求: {url}, 状态码: {response.status_code}")
        return response